namespace DotNetNuke.Services.Scheduling.DNNScheduling
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Services.Scheduling;

    using System;
    using System.Collections;
    using System.Data;
    using System.Runtime.CompilerServices;
    using VB2CS;

    public class SchedulingController
    {
        public int AddSchedule(string TypeFullName, int TimeLapse, string TimeLapseMeasurement, int RetryTimeLapse, string RetryTimeLapseMeasurement, int RetainHistoryNum, string AttachToEvent, bool CatchUpEnabled, bool Enabled, string ObjectDependencies, string Servers)
        {
            return DataProvider.Instance().AddSchedule(TypeFullName, TimeLapse, TimeLapseMeasurement, RetryTimeLapse, RetryTimeLapseMeasurement, RetainHistoryNum, AttachToEvent, CatchUpEnabled, Enabled, ObjectDependencies, Servers);
        }

        public int AddScheduleHistory(ScheduleHistoryItem objScheduleHistoryItem)
        {
            return DataProvider.Instance().AddScheduleHistory(objScheduleHistoryItem.ScheduleID, objScheduleHistoryItem.StartDate, DotNetNuke.Common.Globals.ServerName);
        }

        public void DeleteSchedule(int ScheduleID)
        {
            DataProvider.Instance().DeleteSchedule(ScheduleID);
        }

        public int GetActiveThreadCount()
        {
            return Scheduler.CoreScheduler.GetActiveThreadCount();
        }

        public int GetFreeThreadCount()
        {
            return Scheduler.CoreScheduler.GetFreeThreadCount();
        }

        public int GetMaxThreadCount()
        {
            return Scheduler.CoreScheduler.GetMaxThreadCount();
        }

        public ScheduleItem GetNextScheduledTask(string Server)
        {
            return (ScheduleItem) CBO.FillObject(DataProvider.Instance().GetNextScheduledTask(Server), typeof(ScheduleItem));
        }

        public ArrayList GetSchedule()
        {
            ArrayList arrSource = CBO.FillCollection(DataProvider.Instance().GetSchedule(), typeof(ScheduleHistoryItem));
            ArrayList arrDest = new ArrayList();
            int refIntHelperL0 = arrSource.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                ScheduleItem objScheduleItem = (ScheduleItem) arrSource[i];
                arrDest.Add(objScheduleItem);
            }
            return arrDest;
        }

        public ScheduleItem GetSchedule(int ScheduleID)
        {
            return (ScheduleItem) CBO.FillObject(DataProvider.Instance().GetSchedule(ScheduleID), typeof(ScheduleItem));
        }

        public ArrayList GetSchedule(string Server)
        {
            ArrayList arrSource = CBO.FillCollection(DataProvider.Instance().GetSchedule(Server), typeof(ScheduleHistoryItem));
            ArrayList arrDest = new ArrayList();
            int refIntHelperL0 = arrSource.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                ScheduleItem objScheduleItem = (ScheduleItem) arrSource[i];
                arrDest.Add(objScheduleItem);
            }
            return arrDest;
        }

        public ScheduleItem GetSchedule(string TypeFullName, string Server)
        {
            return (ScheduleItem) CBO.FillObject(DataProvider.Instance().GetSchedule(TypeFullName, Server), typeof(ScheduleItem));
        }

        public ArrayList GetScheduleByEvent(string EventName, string Server)
        {
            ArrayList arrSource = CBO.FillCollection(DataProvider.Instance().GetScheduleByEvent(EventName, Server), typeof(ScheduleHistoryItem));
            ArrayList arrDest = new ArrayList();
            int refIntHelperL0 = arrSource.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                ScheduleItem objScheduleItem = (ScheduleItem) arrSource[i];
                arrDest.Add(objScheduleItem);
            }
            return arrDest;
        }

        public ArrayList GetScheduleHistory(int ScheduleID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetScheduleHistory(ScheduleID), typeof(ScheduleHistoryItem));
        }

        public Hashtable GetScheduleItemSettings(int ScheduleID)
        {
            Hashtable h = new Hashtable();
            IDataReader r = DataProvider.Instance().GetScheduleItemSettings(ScheduleID);
            while (r.Read())
            {
                h.Add((r["SettingName"]), (r["SettingValue"]));
            }
            if (r != null)
            {
                r.Close();
            }
            return h;
        }

        public Collection GetScheduleProcessing()
        {
            return Scheduler.CoreScheduler.GetScheduleInProgress();
        }

        public Collection GetScheduleQueue()
        {
            return Scheduler.CoreScheduler.GetScheduleQueue();
        }

        public ScheduleStatus GetScheduleStatus()
        {
            return Scheduler.CoreScheduler.GetScheduleStatus();
        }

        public void PurgeScheduleHistory()
        {
            DataProvider.Instance().PurgeScheduleHistory();
        }

        public void ReloadSchedule()
        {
            Scheduler.CoreScheduler.ReloadSchedule();
        }

        public void UpdateSchedule(int ScheduleID, string TypeFullName, int TimeLapse, string TimeLapseMeasurement, int RetryTimeLapse, string RetryTimeLapseMeasurement, int RetainHistoryNum, string AttachToEvent, bool CatchUpEnabled, bool Enabled, string ObjectDependencies, string Servers)
        {
            DataProvider.Instance().UpdateSchedule(ScheduleID, TypeFullName, TimeLapse, TimeLapseMeasurement, RetryTimeLapse, RetryTimeLapseMeasurement, RetainHistoryNum, AttachToEvent, CatchUpEnabled, Enabled, ObjectDependencies, Servers);
        }

        public void UpdateScheduleHistory(ScheduleHistoryItem objScheduleHistoryItem)
        {
            DataProvider.Instance().UpdateScheduleHistory(objScheduleHistoryItem.ScheduleHistoryID, objScheduleHistoryItem.EndDate, objScheduleHistoryItem.Succeeded, objScheduleHistoryItem.LogNotes, objScheduleHistoryItem.NextStart);
        }
    }
}

